package com.tangcco.lockwell.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tangcco.lockwell.context.WebContext;
import com.tangcco.lockwell.domain.User;
import com.tangcco.lockwell.domain.UserEventLog;
import com.tangcco.lockwell.domain.other.KeyValue;
import com.tangcco.lockwell.repository.UserEventLogMapper;

import com.tangcco.lockwell.repository.UserMapper;
import com.tangcco.lockwell.service.UserEventLogService;
import com.tangcco.lockwell.utility.DateTimeUtil;
import com.tangcco.lockwell.utility.StudentSubjectUtil;
import com.tangcco.lockwell.viewmodel.admin.user.UserEventPageRequestVM;
import com.tangcco.lockwell.viewmodel.admin.user.UserLearningProgress;
import com.tangcco.lockwell.viewmodel.admin.user.UserProgress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service

public class UserEventLogServiceImpl implements UserEventLogService {
    @Resource
    UserMapper userMapper;
    @Autowired
    UserEventLogMapper userEventLogMapper;
    @Autowired
    WebContext webContext;
    @Resource
    StudentSubjectUtil studentSubjectUtil;
    @Autowired
    public UserEventLogServiceImpl(UserEventLogMapper userEventLogMapper) {
        super();
        this.userEventLogMapper = userEventLogMapper;
    }


    @Override
    public PageInfo<UserEventLog> page(UserEventPageRequestVM requestVM) {
        PageInfo<UserEventLog> pageInfo = PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() ->
                userEventLogMapper.page(requestVM));
        return pageInfo;
    }

    @Override
    public int insert(UserEventLog userEventLog) {
        return userEventLogMapper.insertSelective(userEventLog);
    }

    @Override
    public List<Integer> selectMothCount(String year, String month) {

//        return userEventLogMapper.selectMothCount(year, month);
        List<KeyValue> mouthCount = userEventLogMapper.selectMothCount(year, month);
        List<String> mothStartToNowFormat = DateTimeUtil.MothStartToNowFormat();
        return mothStartToNowFormat.stream().map(md -> {
            KeyValue keyValue = mouthCount.stream().filter(kv -> kv.getName().equals(md)).findAny().orElse(null);
            return null == keyValue ? 0 : keyValue.getValue();
        }).collect(Collectors.toList());
    }

    @Override
    public List<Integer> selectQuestionMothCount(String year, String month) {
        List<KeyValue> mouthCount = userEventLogMapper.selectQuestionMothCount(year, month);
        List<String> mothStartToNowFormat = DateTimeUtil.MothStartToNowFormat();
        return mothStartToNowFormat.stream().map(md -> {
            KeyValue keyValue = mouthCount.stream().filter(kv -> kv.getName().equals(md)).findAny().orElse(null);
            return null == keyValue ? 0 : keyValue.getValue();
        }).collect(Collectors.toList());
    }

    //查询用户打卡天数
    @Override
    public int selectCountByUserName() {
        String userName = webContext.getCurrentUser().getUserName();
        int count = 0;
        if (userName != null){
            count = userEventLogMapper.selectCountByUserName(userName);
        }else {
            count = 0;
        }
        return count;
    }
    //查询用户积分
    @Override
    public Integer selectIntegralByUserName() {
        String userName = webContext.getCurrentUser().getUserName();
        Integer integral = 0;
        if (userName != null ){
            integral = userEventLogMapper.selectIntegralByUserName(userName);
            if (integral == null){
                integral = 1000;
            }
        }
        return integral;
    }

    @Override
    public Integer selectKeepLearning() {
        String userName = webContext.getCurrentUser().getUserName();
        return userEventLogMapper.selectKeepLearning(userName);
    }

    @Override
    public Double selectProgressByUserName() {
        User user = webContext.getCurrentUser();
        Integer userId = user.getUserId();
        String optSubject = user.getOptSubject();
        if(optSubject == null){
            optSubject = studentSubjectUtil.getDefaultSubjectId();
        }
        String[] split = optSubject.split(",");
        List<Integer> subjectIds = new ArrayList<>();
        for (String s : split){
            subjectIds.add(Integer.parseInt(s));
        }
        UserProgress userProgress = new UserProgress(userId,subjectIds);
        Double progress = 0.0;
        List<UserLearningProgress> userLearningProgresses = userMapper.selectLearningProgress(userProgress);
        for (UserLearningProgress userLearningProgress : userLearningProgresses){
            progress  = progress+userLearningProgress.getProgress();
        }
//        Integer result = (int) (progress/userLearningProgresses.size());
        Double result = Math.round((progress / userLearningProgresses.size()) * 100.0) / 100.0;
        return result;
    }
}





